package com.ma.springframework.beans.factory.support;

import com.ma.springframework.beans.BeansException;
import com.ma.springframework.beans.factory.FactoryBean;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry{
    private final Map<String,Object> factoryBeanObjectCache = new ConcurrentHashMap<>();

    public Object getCacheObjectForFactoryBean(String beanName) {
        Object object = this.factoryBeanObjectCache.get(beanName);
        return (object != NULL_OBJECT  ? object :null);
    }
    protected Object getObjectFromFactoryBean(FactoryBean factoryBean,String beanName){
        if (factoryBean.isSingleton()){
            Object object = this.factoryBeanObjectCache.get(beanName);
            if (object ==null){
                object = doGetObjectFromFactoryBean(factoryBean,beanName);
                this.factoryBeanObjectCache.put(beanName,(object!=null?object:NULL_OBJECT ));
            }
            return (object!=NULL_OBJECT ?object:null);
        }else {
            return doGetObjectFromFactoryBean(factoryBean,beanName);
        }
    }

    private Object doGetObjectFromFactoryBean(FactoryBean factoryBean, String beanName) {
        try {
            return factoryBean.getObject();
        } catch (Exception e) {
             throw new BeansException("FactoryBean throw exception on object["+beanName+"] creation",e);
        }
    }
}
